package com.zlq.iwork.bean;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.zlq.iwork.db.DBHelper;

import android.R.integer;
import android.content.Context;

public class NoteDao {

	private Context context;
	private Dao<Note, integer> noteDao;
	private DBHelper helper;

	public NoteDao(Context context) {
		this.context = context;
		try {
			helper = DBHelper.getHelper(context);
			noteDao = helper.getDao(Note.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void add(Note note) {
		try {
			noteDao.create(note);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<Note> findAll() {
		List<Note> list = new ArrayList<Note>();
		try {
			list=noteDao.queryBuilder().orderBy("date", false).query();
			//list = noteDao.queryForAll();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public Note findById(integer id) {
		try {
			Note note = noteDao.queryForId(id);
			return note;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
